package com.zhonghui.common.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.zhonghui.common.eshopUtil.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration(proxyBeanMethods = false)
public class SmsUtils {
    /**
     * 需要记住的单词：phoneNumbers templateParam ClientException
     * DefaultProfile.getProfile
     * new DefaultAcsClient
     * CommonRequest
     * getCommonResponse
     */
    private static RedisUtils redisUtils;

    @Autowired
    public SmsUtils(RedisUtils redisUtils){ SmsUtils.redisUtils = redisUtils;
    }

    public static void sendSms(String phoneNumbers,String templateParam) throws ClientException {
        String regionId = redisUtils.getY("sms_region");
        String accessKeyId = redisUtils.getY("sms_access_key");
        String accessKeySecret= redisUtils.getY("sms_access_secret");
        String sign = redisUtils.getY("sms_sign");

        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setVersion("2017-05-25");
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setAction("SendSms");

        request.putQueryParameter("RegionId",regionId);
        request.putQueryParameter("PhoneNumbers",phoneNumbers);
        request.putQueryParameter("TemplateParam",templateParam);
        request.putQueryParameter("SignName",sign);

        CommonResponse response = client.getCommonResponse(request);
        System.out.printf(response.getData());
    }
}
